import sys
from PySide6.QtWidgets import QTreeView, QTabWidget, QTableView, QPushButton, QSplitter, QVBoxLayout, QHBoxLayout, QWidget, QApplication, QMainWindow
from PySide6.QtGui import QStandardItemModel, QStandardItem
from PySide6.QtCore import Qt

class DictTreeModel(QStandardItemModel):
    """将字典数据转换为树形模型的类"""
    
    def __init__(self):
        super().__init__()
        self.setHorizontalHeaderLabels(["键", "值"])
    
    def populate(self, data, parent=None):
        """递归填充树形模型"""
        if parent is None:
            parent = self.invisibleRootItem()
            
        if isinstance(data, dict):
            for key, value in data.items():
                key_item = QStandardItem(str(key))
                value_item = QStandardItem("")
                
                # 根据值的类型设置显示
                if isinstance(value, dict):
                    value_item.setText(f"字典 ({len(value)} 项)")
                    parent.appendRow([key_item, value_item])
                    self.populate(value, key_item)
                elif isinstance(value, list):
                    value_item.setText(f"列表 ({len(value)} 项)")
                    parent.appendRow([key_item, value_item])
                    self._populate_list(value, key_item)
                else:
                    value_item.setText(str(value))
                    parent.appendRow([key_item, value_item])
        else:
            item = QStandardItem(str(data))
            parent.appendRow(item)
    
    def _populate_list(self, data, parent):
        """处理列表数据"""
        for i, value in enumerate(data):
            index_item = QStandardItem(f"[{i}]")
            value_item = QStandardItem("")
            
            if isinstance(value, dict):
                value_item.setText(f"字典 ({len(value)} 项)")
                parent.appendRow([index_item, value_item])
                self.populate(value, index_item)
            elif isinstance(value, list):
                value_item.setText(f"列表 ({len(value)} 项)")
                parent.appendRow([index_item, value_item])
                self._populate_list(value, index_item)
            else:
                value_item.setText(str(value))
                parent.appendRow([index_item, value_item])
    
    def _get_icon(self, icon_type):
        """返回对应类型的图标（简化版，实际应用中可以使用真实图标）"""
        return None
